package guowenlong.golf.ui.friendlymatch;

/**
 * des   : 赛道自定义view
 * author: Administrator
 * email : guowenlong20000@gmail.com
 * time  : 2017年04月27日 下午 11:52.
 */


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

/**
 * Created by real on 8/8/16.
 */
public class LineView extends View {

  public static final int TYPE_HOLE = 0 << 1;
  public static final int TYPE_T = 0 << 2;

  private Paint mPaint;
  private Paint mTextPaint;

  private float mStartX, mStartY, mEndX, mEndY;

  private int mComputedType = TYPE_HOLE;
  private int mOriginalYD;
  private int mDistanceBetweenHoleAndT;

  public LineView(Context context, int originalYD, int computedType) {
    super(context);
    mComputedType = computedType;
    mOriginalYD = originalYD;

    mPaint = new Paint();
    mPaint.setColor(Color.RED);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);

    mTextPaint = new Paint();
    mTextPaint.setColor(Color.WHITE);
    mTextPaint.setTextSize(20);
  }

  public void setDistanceBetweenHoleAndT(int distanceBetweenHoleAndT) {
    mDistanceBetweenHoleAndT = distanceBetweenHoleAndT;
  }

  public void drawLine(float startX, float startY, float endX, float endY) {
    mStartX = startX;
    mStartY = startY;
    mEndX = endX;
    mEndY = endY;

    invalidate();
    Log.i("LineView",
        "mStartX is (" + mStartX + "), mStartY is (" + mStartY + "), mEndX is (" + mEndX
            + "), mEndY is (" + mEndY + ")");
  }


  @Override
  public void draw(Canvas canvas) {
    super.draw(canvas);
    canvas.drawLine(mStartX, mStartY, mEndX, mEndY, mPaint);
    int distanceInDevice = getDistanceInDevice();
    if (mDistanceBetweenHoleAndT > 0) {
      int distance = (int) (distanceInDevice * ((double) mOriginalYD
          / (double) mDistanceBetweenHoleAndT));
//            switch (mComputedType){
//                case TYPE_T:
//                    distance = mOriginalYD - distance;
//                    Log.i("LineView", "mOriginalYD = " + mOriginalYD + " @@ distance = " + distance);
//                    break;
//            }
      canvas.drawText(distance + "YD", getTextStartX(), getTextStartY(), mTextPaint);
    }
    Log.i("LineView", "draw");
  }

  private float getTextStartX() {
    float startX = mStartX + (mEndX - mStartX) / 2;
    return startX;
  }

  private float getTextStartY() {
    float startY = mStartY + (mEndY - mStartY) / 2;
    return startY;
  }

  private int getDistanceInDevice() {

    float xDist = mEndX - mStartX;
    float yDist = mEndY - mStartY;

    double distance = Math.sqrt((xDist * xDist) + (yDist * yDist));

    return (int) Math.round(distance);
  }
}
